<%def name="inline_edit(inline_admin_formset, formset_count)">

<%!
    # filter to titlecase text
    def title(text):
        return text.title()
%>

<%namespace name="admin" module="django.contrib.admin.templatetags.adminmedia" />

<div class="inline-group" id="${ inline_admin_formset.formset.prefix }-group">
  <h2>${ inline_admin_formset.opts.verbose_name_plural|title,h }</h2>
${inline_admin_formset.formset.management_form}
${inline_admin_formset.formset.non_form_errors()}
<%
    inline_admin_formset_list = [X for X in inline_admin_formset]
%>
% for i,inline_admin_form in enumerate(inline_admin_formset_list):
<%
    lastloop = (i == ( len(inline_admin_formset_list)-1) )
%>
<div class="inline-related${ ' empty-form last-related' if lastloop else '' }" id="${ inline_admin_formset.formset.prefix }-${i if lastloop else 'empty'}">
##  <h3><b>${inline_admin_formset.opts.verbose_name|title,h}:</b>&nbsp;<span class="inline_label">${ inline_admin_form.original if inline_admin_form.original else '#'+str(i) }</span>
##    % if inline_admin_form.show_url:
##        <a href="../../../r/${ inline_admin_form.original_content_type_id }/${ inline_admin_form.original.id }/">${ trans("View on site") }</a>
##    % endif

% if inline_admin_formset.formset.can_delete and inline_admin_form.original:
<span class="delete">${inline_admin_form.deletion_field().field.as_widget()} ${inline_admin_form.deletion_field().label_tag()}</span>
% endif

  </h3>

% if inline_admin_form.show_url:

  <p><a href="../../../r/${inline_admin_form.original.content_type_id}/${inline_admin_form.original.id}/">${trans("View on site")}</a></p>

% endif


## FJ was admin/includes/fieldset.html
<%namespace file="admin/dm1/includes/fieldset.html" import="*" />
% for fieldset in inline_admin_form:
    ${fieldsets(fieldset)}
% endfor

% if inline_admin_form.has_auto_field:
  ${inline_admin_form.pk_field().field}
% endif
  ${inline_admin_form.fk_field().field}
</div>
% endfor
</div>

<script type="text/javascript">
(function($) {
    $(document).ready(function() {
        var rows = "#${ inline_admin_formset.formset.prefix }-group .inline-related";
        var updateInlineLabel = function(row) {
            $(rows).find(".inline_label").each(function(i) {
                var count = i + 1;
                $(this).html($(this).html().replace(/(#\d+)/g, "#" + count));
            });
        }
        var reinitDateTimeShortCuts = function() {
            // Reinitialize the calendar and clock widgets by force, yuck.
            if (typeof DateTimeShortcuts != "undefined") {
                $(".datetimeshortcuts").remove();
                DateTimeShortcuts.init();
            }
        }
        var updateSelectFilter = function() {
            // If any SelectFilter widgets were added, instantiate a new instance.
            if (typeof SelectFilter != "undefined"){
                $(".selectfilter").each(function(index, value){
                  var namearr = value.name.split('-');
                  SelectFilter.init(value.id, namearr[namearr.length-1], false, "${ admin_media_prefix() }");
                })
                $(".selectfilterstacked").each(function(index, value){
                  var namearr = value.name.split('-');
                  SelectFilter.init(value.id, namearr[namearr.length-1], true, "${ admin_media_prefix() }");
                })
            }
        }
        var initPrepopulatedFields = function(row) {
            row.find('.prepopulated_field').each(function() {
                var field = $(this);
                var input = field.find('input, select, textarea');
                var dependency_list = input.data('dependency_list') || [];
                var dependencies = row.find(dependency_list.join(',')).find('input, select, textarea');
                if (dependencies.length) {
                    input.prepopulate(dependencies, input.attr('maxlength'));
                }
            });
        }
        $(rows).formset({
            prefix: "${ inline_admin_formset.formset.prefix }",
            addText: "Add another ${title(inline_admin_formset.opts.verbose_name)}",
            numCols: "${inline_admin_form.field_count()}",
            formCssClass: "dynamic-${ inline_admin_formset.formset.prefix }",
            deleteCssClass: "inline-deletelink",
            deleteText: "${ trans('Remove')}",
            emptyCssClass: "empty-form",
            removed: updateInlineLabel,
            added: (function(row) {
                initPrepopulatedFields(row);
                reinitDateTimeShortCuts();
                updateSelectFilter();
                updateInlineLabel(row);
            })
        });
    });
})(django.jQuery);
</script>

</%def>